'''
This program plots the graph b/w number of users and the pageValues. 
'''


import matplotlib.pyplot as plt
import readLinks as rL;

import numpy as np
from scipy.optimize import curve_fit

import config as cg

from extraFns import normalize
from extraFns import reduction


def fit(x, a, b, c):
	return a*np.exp(-b*x) + c
	
	
	
def plotFit( x, y, Label, Color ):
	# calculate polynomial
	z = np.polyfit(x, y, 40)
	f = np.poly1d(z)

	# calculate new x's and y's
	x_new = np.linspace(x[0], x[-1], 50)
	y_new = f(x_new)
	#plt.plot(x ,y, 'r' );
	lines = plt.plot(x_new, y_new, Color, label=Label);
	plt.xlabel('No of Users');
	plt.ylabel('Value of Page');
	return lines;
	

def plotQuadratic(n):
	
	x = []
	y = []
	z = []
	i = 0;
	for i in range(1,int(n)):
		x.append(i)
		y.append(i*i);
		z.append(i*i*i);

	plt.plot(x, x, 'b', label="x");	
	#plt.legend(loc = 'upper left')
	plt.plot(x, y, 'g', label="x^2");	
	#plt.legend(loc = 'upper right')
	plt.plot(x, z, 'y', label="x^3");
	plt.legend(loc = 'upper center')	
	



def plotMetric(x, y, title):
	plt.title(title);
	y_max = max(y)
	y_min = min(y)
	x_max = max(x)
	plt.ylim(y_min, y_max)
	plt.xlim(1, x_max)
	
	plotFit(x, y, 'val()', 'r');
	plotQuadratic(x_max)
	plt.savefig(cg.graph + title + '.png', dpi=200);
	plt.clf()	



def plotAll():
	age, freq, freqR, ageR, freAvg, ageAvg, n = rL.getXYZ();
	size, n1 = rL.getXY()

	ageAvg = reduction(ageAvg);

	plotMetric(n1, size, "Trivial Approach")
	plotMetric(n, age, "Age-Approach")
	plotMetric(n, freq, "Age-Freq Approach")
	plotMetric(n, freqR, "RootFreq Approach")
	plotMetric(n, ageR, "Root-Age Approach")
	plotMetric(n, freAvg, "Average-Freq Approach")
	plotMetric(n, ageAvg, "Average-Age Approach")


		

#plotTrivial()
plotAll()
#plotCombined()
#plotTrivial()
#plotAge()
#plotFreqAge()
